package main

import (
	"container/heap"
	"fmt"
)

func main() {
	h := IntHeap{5, 6, 3, 2, 7, 4, 9, 8, 1, 6}
	heap.Init(&h)
	fmt.Println(h)
	for h.Len() > 0 {
		fmt.Println(heap.Pop(&h))
	}
	fmt.Println(h)
}

type IntHeap []int

func (h *IntHeap) Len() int {
	return len(*h)
}

func (h *IntHeap) Less(i, j int) bool {
	return (*h)[i] < (*h)[j]
}

func (h *IntHeap) Swap(i, j int) {
	(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}

func (h *IntHeap) Push(x any) {
	*h = append(*h, x.(int))
}

func (h *IntHeap) Pop() any {
	v := (*h)[len(*h)-1]
	*h = (*h)[:len(*h)-1]
	return v
}
